﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using TPDF.Enums;

namespace TPDF
{
    public class TPage
    {
        public TPage(IntPtr intPtr, int pageIndex)
        {
            document = intPtr;
            Index = pageIndex;
            if (intPtr == IntPtr.Zero) throw new ArgumentNullException("当前文档对象为空指针");
            PageIntPtr = TpdfHelper.GetPage(document, pageIndex);
            if (PageIntPtr == IntPtr.Zero) throw new Exception("当前页面指针为空!");
            Height = TpdfHelper.GetPageHeight(PageIntPtr);
            Width = TpdfHelper.GetPageWidth(PageIntPtr);
        }
        public double Height { get; set; }
        public double Width { get; set; }
        public int Index { get; set; }
        public PageOrientations Orientations { get; set; } = PageOrientations.Normal;
        public RenderingFlags RenderingFlags { get; set; } = RenderingFlags.None;
        public void Render(WriteableBitmap writeableBitmap)
        {
            if (0 < writeableBitmap.PixelWidth && 0 < writeableBitmap.PixelHeight)
            {
                BitmapFormats bitmapFormat = GetBitmapFormat(writeableBitmap.Format);
                writeableBitmap.Lock();
                var dataIntPtr = TpdfHelper.FPDFBitmapCreateEx((int)Width, (int)Height, (int)bitmapFormat, writeableBitmap.BackBuffer, writeableBitmap.BackBufferStride);
                TpdfHelper.FPDF_RenderPageBitmap(dataIntPtr, PageIntPtr, 0, 0, (int)Width, (int)Height, (int)Orientations, (int)RenderingFlags);
                int left = 0;
                int top = 0;
                int width = (int)Width;
                int height = (int)Height;
                int num = left;
                int num2 = top;
                int num3 = width;
                int num4 = height;
                if (num < 0)
                {
                    num3 += num;
                    num = 0;
                }

                if (num2 < 0)
                {
                    num4 += num2;
                    num2 = 0;
                }

                num3 = Math.Min(num3, writeableBitmap.PixelWidth);
                num4 = Math.Min(num4, writeableBitmap.PixelHeight);
                writeableBitmap.AddDirtyRect(new Int32Rect(num, num2, num3, num4));
                writeableBitmap.Unlock();
                TpdfHelper.ClosePage(PageIntPtr);
                TpdfHelper.DestoryBitmap(dataIntPtr);
            }
        }
        IntPtr document = IntPtr.Zero;
        IntPtr PageIntPtr = IntPtr.Zero;
        private static BitmapFormats GetBitmapFormat(PixelFormat pixelFormat)
        {
            if (pixelFormat == PixelFormats.Bgra32)
            {
                return BitmapFormats.FPDFBitmap_BGRA;
            }

            if (pixelFormat == PixelFormats.Bgr32)
            {
                return BitmapFormats.FPDFBitmap_BGRx;
            }

            if (pixelFormat == PixelFormats.Bgr24)
            {
                return BitmapFormats.FPDFBitmap_BGR;
            }

            if (pixelFormat == PixelFormats.Gray8)
            {
                return BitmapFormats.FPDFBitmap_Gray;
            }

            throw new NotSupportedException($"Pixel format {pixelFormat} is not supported.");
        }
    }
}
